﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : Actor, IDamage {
    /// <summary>
    /// 保存之前走过的平台
    /// </summary>
    public BoxCollider2D contentBox;
    float time;
    public Vector2 halfSizeclimp = new Vector2 (0.4f, 0.4f);
    public float AttackCoolTime = 1;
    float AttackTimer = 1;
    bool hasAttack = true;
    public float attackDis = 10f;

    //当前状态
    private int index;

    protected override void Awake () {
        base.Awake ();
        actorInfo = new ActorInfo (0, "111", 5, 0.5f, 1);
    }
    void Start () {
        StartCoroutine (EnemyMotion ());
    }

    IEnumerator EnemyMotion () {
        yield return null;
        float t = 0;
        float time = 0;
        while (!isDeath) {

            if (t < time) {
                t += Time.deltaTime;
                index = CheckPlatfrom (index);
                index = CheckPlayerIsPlatform (index);
                Move (index);
            } else {
                time = UnityEngine.Random.Range (2.0f, 4.0f);
                index = GetAction ();
                t = 0;
            }
            yield return null;

        }
    }
    int CheckPlatfrom (int index) {
        if (contentBox == null || index == 0) {
            return 0;

        }
        float left = contentBox.transform.position.x + contentBox.offset.x - contentBox.size.x * halfSizeclimp.x;
        float right = contentBox.transform.position.x + contentBox.offset.x + contentBox.size.x * halfSizeclimp.y;

        if (transform.position.x < left) {
            return 1;
        } else if (transform.position.x > right) {
            return -1;
        }
        return index;
    }

    int CheckPlayerIsPlatform (int index) {
        if (contentBox == null || GameManager.Instance.PlayerCtrl == null)
            return index;
        Transform player = GameManager.Instance.PlayerCtrl.transform;
        float left = contentBox.transform.position.x + contentBox.offset.x - contentBox.size.x * halfSizeclimp.x;
        float right = contentBox.transform.position.x + contentBox.offset.x + contentBox.size.x * halfSizeclimp.y;
        if (player.position.x < right && player.position.x > left) {
            if (player.position.x < transform.position.x) {
                return -1;
            } else if (player.position.x > transform.position.x) {
                return 1;
            }

        }
        return index;
    }

    private int GetAction () {

        float rand = UnityEngine.Random.Range (0.0f, 1.0f);
        if (rand > 0.8f) {
            return -1;
        } else if (rand > 0.2f) {
            return 0;
        } else {
            return 1;
        }

    }

    protected virtual void ToAttack () {
        Debug.Log ("Attack");
    }

    public virtual void Move (int index) {
        if (index == 0) {
            rig.velocity = Vector2.zero;
            return;
        }
        if (index == 1) {
            spriteRenderer.flipX = true;

        } else if (index == -1) {
            spriteRenderer.flipX = false;

        }
        rig.velocity = new Vector2 (actorInfo.moveSpeed * forward, 0);

    }

    public virtual void ToDeath () {
        Destroy (gameObject, 0.5f);
    }

    public void OnDamage (GameObject sender, Vector3 pos, int Attack) {
        if (isDeath) {
            return;
        }
        actorInfo.TakeHp (Attack);
        animator.SetTrigger ("Hit");
        Vector2 dir = transform.position - pos;
        dir.Normalize ();
        if (actorInfo.hp <= 0) {
            sender.GetComponent<Character> ().AddCoin (2);
            ToDeath ();
        }
        GameObjectPool.instance.CreateObject ("HitEffect2", Resources.Load<GameObject> ("Prefabs/Effect/HitEffect2"), transform.position, Quaternion.identity);
        AudioManager.Instance.PlayAudio(Resources.Load<AudioClip>("Audio/enemy_damage"));
        
        StartCoroutine (ToRepel (dir, 0.5f));
    }
    void Update () {

    }

    public void OnCollisionEnter2D (Collision2D collision) {
        if (collision.transform.tag == "Contact") {
            contentBox = collision.collider as BoxCollider2D;
        }

    }
    private void OnCollisionStay2D (Collision2D collision) {
        if (collision.transform.tag == "Player") {
           
            if (hasAttack&&!isHit) {
                var dam = collision.transform.GetComponent<IDamage> ();
                if (dam != null) {
                    dam.OnDamage (gameObject, transform.position, actorInfo.attack);
                    ToAttack ();
                    AttackTimer = AttackCoolTime;
                    hasAttack = false;
                }
            }

        }
        if (!hasAttack) {
            AttackTimer -= Time.fixedDeltaTime;
            if (AttackTimer <= 0) {
                hasAttack = true;
            }
        }
    }

}